//
//  WSPlayerToolBar.m
//  musicPalyer
//
//  Created by ws on 2019/2/14.
//  Copyright © 2019年 王松. All rights reserved.
//

#import "WSPlayerToolBar.h"
#import "UIButton+CZ.h"
#import "UIImage+CZ.h"
#import "WSMusic.h"
#import "WSMusicTool.h"
#import "NSString+ws.h"
@interface WSPlayerToolBar()
@property (weak, nonatomic) IBOutlet UIImageView *singerImageView;//歌手的头像
@property (weak, nonatomic) IBOutlet UILabel *musicNameLabel;//歌曲的名字
@property (weak, nonatomic) IBOutlet UILabel *singerNameLabel;//歌手的名字
@property (weak, nonatomic) IBOutlet UISlider *timeSlider;
@property (weak, nonatomic) IBOutlet UILabel *totalTimeLabel;//总时间
@property (weak, nonatomic) IBOutlet UILabel *currentTimeLabel;//当前播放的时间
@property(strong,nonatomic)CADisplayLink *link;//定时器
@property(assign,nonatomic,getter=isDragging)BOOL dragging;//是否正在拖拽
@end

@implementation WSPlayerToolBar
-(CADisplayLink *)link{
    if (!_link) {
        _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(ws_update)];
    }
    return _link;
}


+(instancetype)playerToolBar{
    return [[[NSBundle mainBundle] loadNibNamed:@"WSPlayerToolBar" owner:nil options:nil] lastObject];
}
#pragma mark 播放状态按钮点击事件
- (IBAction)playBtnClick:(id)btn {
    //更改播放状态
    self.playing = !self.isPlaying;
    if (self.isPlaying) {
        //播放音乐
//        NSLog(@"播放音乐");
        //1.如果是播放的状态 按钮图片更改为暂停的状态
        [btn setNBg:@"playbar_pausebtn_nomal" hBg:@"playbar_pausebtn_click"];
        //通知代理
        [self ws_notifyDelegateWithBtnType:BtnTYpePlay];
        
        
    }else{
        //暂停音乐
//        NSLog(@"暂停音乐");
        //2.如果当前是暂停的状态 按钮图片更改为播放的状态
        [btn setNBg:@"playbar_playbtn_nomal" hBg:@"playbar_playbtn_click"];
        
        //通知代理
        [self ws_notifyDelegateWithBtnType:BtnTypePause];
    }
}

#pragma mark 上一首按钮点击事件
- (IBAction)previousBtnClick:(id)sender {
    //通知代理
    [self ws_notifyDelegateWithBtnType:BtnTypePrevious];
    //恢复头像的位置
    self.singerImageView.transform = CGAffineTransformIdentity;
}
#pragma mark 下一首按钮点击事件
- (IBAction)nextBtnClick:(id)sender {
    //通知代理
    [self ws_notifyDelegateWithBtnType:BtnTypeNext];
    //恢复头像的位置
    self.singerImageView.transform = CGAffineTransformIdentity;
}


#pragma mark 设置当前播放的音乐 并显示数据
-(void)setPlayingMusicModel:(WSMusic *)playingMusicModel{
    _playingMusicModel = playingMusicModel;
    //歌手的头像
    UIImage *circleImage = [UIImage circleImageWithName:playingMusicModel.singerIcon borderWidth:1.0 borderColor:[UIColor yellowColor]];
    self.singerImageView.image = circleImage;
    //    self.singerImageView.image = [UIImage imageNamed:playingMusicModel.singerIcon];
    //歌曲名字
    self.musicNameLabel.text = playingMusicModel.name;
    //歌手名字
    self.singerNameLabel.text = playingMusicModel.singer;
    //设置总时间
    double duration = [WSMusicTool ws_musicTool].ws_player.duration;
    self.totalTimeLabel.text = [NSString ws_getMinuteSecondWithSecond:duration];
    //设置slider的最大值
    self.timeSlider.maximumValue = duration;
    //重置slider的播放时间
    self.timeSlider.value = 0;
    //头像复位 -- 自动播放下一首的时候
    self.singerImageView.transform = CGAffineTransformIdentity;
}

#pragma mark  抽取代理--封装
-(void)ws_notifyDelegateWithBtnType:(BtnType)btnType{
    //通知代理
    if ([self.delegate respondsToSelector:@selector(ws_playToolBar:btnClickWtihType:)]) {
        [self.delegate ws_playToolBar:self btnClickWtihType:btnType];
    }
}

-(void)awakeFromNib{
    [super awakeFromNib];
    //设置slider 按钮的图片
    [self.timeSlider setThumbImage:[UIImage imageNamed:@"playbar_slider_thumb"] forState:UIControlStateNormal];
    
    //开启定时器
    [self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}
-(void)dealloc{
    //移除定时器
    [self.link removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}


#pragma mark 更新进度条
-(void)ws_update{
    if (self.isPlaying && self.isDragging == NO) {//如果正在播放，才有需要做下面的操作
        //1.更新进度条
        double currentTime = [WSMusicTool ws_musicTool].ws_player.currentTime;
        self.timeSlider.value = currentTime;
        
        //2.更新时间
        self.currentTimeLabel.text = [NSString ws_getMinuteSecondWithSecond:currentTime];
        
        //3.头像转动
        CGFloat angle = M_PI_4 / 60;
        self.singerImageView.transform = CGAffineTransformRotate(self.singerImageView.transform, angle);
    }
    
}
#pragma mark slider点击的时候暂停播放
- (IBAction)stopPlayAction:(UISlider *)sender {
    //更改拖拽的状态
    self.dragging = YES;
    
    [[WSMusicTool ws_musicTool] ws_pause];
}
#pragma mark slider拖动
- (IBAction)sliderChangedAction:(UISlider *)sender {
    //1.播放器的进度
    [WSMusicTool ws_musicTool].ws_player.currentTime = sender.value;
    
    //2.工具条的当前时间
    self.currentTimeLabel.text = [NSString ws_getMinuteSecondWithSecond:sender.value];
}

#pragma mark slider 松开手指的时候，继续播放
- (IBAction)replayAction:(id)sender {
    self.dragging = NO;
    
    if (self.isPlaying) {
        [[WSMusicTool ws_musicTool] ws_play];
    }
}

@end
